#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
import logging
import traceback
from mq.queue import BasicQueue


# 采用redis队列
class RdsQueue(BasicQueue):
    def __init__(self, rds):
        self.rds = rds

    # data 为dict或者list类型， 需要json序列化
    def push(self, queue_name, data, extra=None):
        result = None
        try:
            if isinstance(data, dict) or isinstance(data, list):
                result = self.rds.rpush(queue_name, json.dumps(data))
            else:
                result = self.rds.rpush(queue_name, data)
        except Exception as e:
            logging.error(e)
            logging.error(traceback.format_exc())
        return result

    def pop(self, queue_name, extra=None):
        result = None
        try:
            result = self.rds.lpop(queue_name)
            if result is not None:
                result = json.loads(result)
        except json.decoder.JSONDecodeError:
            return result
        except Exception as e:
            logging.error(e)
            logging.error(traceback.format_exc())
        return result
